package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itheima.constant.MessageConstant;
import com.itheima.dao.CheckGroupDao;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.CheckGroup;
import com.itheima.service.CheckGroupService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

@Service(interfaceClass = CheckGroupService.class)
@Transactional
public class CheckGroupServiceImpl implements CheckGroupService {

    @Autowired
    private CheckGroupDao checkGroupDao;

    @Override
    public Result add(CheckGroup checkGroup, Integer[] ids) {
        try {
            checkGroupDao.add(checkGroup);
            Integer checkGroupId = checkGroup.getId();
            for (Integer id : ids) {
                checkGroupDao.addCheckGroupIdCheckAndCheckItemId(checkGroupId, id);
            }
        } catch (Exception e) {
            return new Result(false, MessageConstant.ADD_CHECKGROUP_FAIL);
        }
        return new Result(true, MessageConstant.ADD_CHECKGROUP_SUCCESS);

    }

    @Override
    public PageResult findPage(QueryPageBean queryPageBean) {
        PageHelper.startPage(queryPageBean.getCurrentPage(), queryPageBean.getPageSize());
        List<CheckGroup> checkGroupList;
        if (queryPageBean.getQueryString() == null || "".equals(queryPageBean.getQueryString())) {
            checkGroupList = checkGroupDao.findPageAll();
        } else {
            checkGroupList = checkGroupDao.findPage(queryPageBean.getQueryString());
        }
        PageInfo pageInfo = new PageInfo<>(checkGroupList);
        return new PageResult(pageInfo.getTotal(), checkGroupList);
    }

    @Override
    public Result findById(Integer id) {
        CheckGroup checkGroup = null;
        try {
            checkGroup = checkGroupDao.findById(id);
        } catch (Exception e) {
            return new Result(false, MessageConstant.QUERY_CHECKGROUP_FAIL, checkGroup);
        }

        return new Result(true, MessageConstant.QUERY_CHECKGROUP_SUCCESS, checkGroup);


    }

    @Override
    public Result findCheckItemIdsByCheckGroupId(Integer checkGroupId) {
        List<Integer> list = null;
        try {
            list = checkGroupDao.findCheckItemIdsByCheckGroupId(checkGroupId);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_CHECKITEM_FAIL, list);
        }

        return new Result(true, MessageConstant.QUERY_CHECKITEM_SUCCESS, list);

    }

    @Override
    public Result addCheckGroupIdAndCheckItem(CheckGroup checkGroup, Integer[] checkItemIds) {
        try {
            checkGroupDao.update(checkGroup);
            checkGroupDao.delete(checkGroup.getId());
            for (Integer checkItemId : checkItemIds) {
                checkGroupDao.addCheckGroupIdAndCheckItem(checkGroup.getId(), checkItemId);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(true, MessageConstant.EDIT_CHECKGROUP_FAIL);
        }

        return new Result(true, MessageConstant.EDIT_CHECKGROUP_SUCCESS);
    }

    @Override
    public Result findPageAll() {
        List<CheckGroup> checkGroupList = null;
        try {
            checkGroupList = checkGroupDao.findPageAll();
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_CHECKGROUP_FAIL, checkGroupList);
        }

        return new Result(true, MessageConstant.QUERY_CHECKGROUP_SUCCESS, checkGroupList);

    }

    @Override
    public Result delete(Integer id) {
        try {
            checkGroupDao.delete(id);
            checkGroupDao.deleteCheckGroup(id);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.DELETE_CHECKGROUP_FAIL);
        }
        return new Result(false,MessageConstant.DELETE_CHECKGROUP_SUCCESS);

    }


}
